package tk.leafotto.sign2.utils;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import tk.leafotto.sign2.bean.Player;
import tk.leafotto.sign2.bean.Sign;
import tk.leafotto.sign2.dao.DbUtils;

public class DataUtil {

    public void importData(Context context,String fileName) {
        String[] dataJson = new FileUtil().restoreFromSD(fileName);
        //判断字符串是否是2个
        if (dataJson != null && dataJson.length > 1) {
            Gson gson = new Gson();
            ArrayList<Player> playerList = gson.fromJson(dataJson[0], new TypeToken<ArrayList<Player>>(){}.getType()) ;
            ArrayList<Sign> signList = gson.fromJson(dataJson[1], new TypeToken<ArrayList<Sign>>(){}.getType()) ;
            DbUtils db = DbUtils.instance(context);
            //清空数据
            db.cleanData();
            //恢复数据
            db.restorePlayer(playerList);
            db.restoreSign(signList);
        }
    }
    public int exportData(Context context){
        DbUtils db = DbUtils.instance(context);
        ArrayList<Player> allPlayerData = db.getAllPlayerData();
        ArrayList<Sign> allSignData = db.getAllSignData();
        Gson gson = new Gson();
        String allPlayerDataJson = gson.toJson(allPlayerData);
        String allSignDataJson = gson.toJson(allSignData);
        return new FileUtil().save2SD(allPlayerDataJson + "\r\n" + allSignDataJson);
    }
}
